package com.example.skillcinema.ui.screens.main

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.example.skillcinema.R
import com.example.skillcinema.adapters.FilmRecyclerViewAdapter
import com.example.skillcinema.databinding.FragmentStaffDetailBinding
import com.example.skillcinema.databinding.FragmentStaffListBinding
import com.example.skillcinema.db.enums.FilmFetchType
import com.example.skillcinema.ui.screens.main.FilmAllFragment.Companion.FILM_FETCH_TYPE_ARG
import com.example.skillcinema.ui.screens.main.FilmDetailsFragment.Companion.FILM_ID_ARG
import com.example.skillcinema.ui.screens.main.StaffFilmographyFragment.Companion.STAFF_NAME_ARG
import com.example.skillcinema.ui.screens.main.viewmodels.StaffDetailViewModel
import com.example.skillcinema.utils.sharemodels.FilmDataToShare
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class StaffDetailFragment : Fragment() {
    private var _binding: FragmentStaffDetailBinding? = null
    private val binding: FragmentStaffDetailBinding
        get() = _binding!!
    private val viewModel: StaffDetailViewModel by viewModels<StaffDetailViewModel>()
    private var staffId: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            staffId = it.getInt(STAFF_ID_ARG)
            viewModel.loadData(staffId)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentStaffDetailBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (activity as? AppCompatActivity)?.setSupportActionBar(binding.toolbar)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayShowTitleEnabled(false)
        binding.toolbar.title = ""
        binding.toolbar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }
        val topFilmsAdapter: FilmRecyclerViewAdapter =
            FilmRecyclerViewAdapter({ id -> filmDetail(id) }, { showTopFilmCollection() })
        binding.staffTopFilmRecyclerView.adapter = topFilmsAdapter
        binding.showAllFilmsButton.setOnClickListener {
            showTopFilmCollection()
        }
        binding.showAllButton.setOnClickListener {
            showAllCollectionFilms()
        }
        with(binding) {
            lifecycleScope.run {
                launch {
                    viewModel.staffDetailFlow.collect { item ->
                        item?.let {
                            actorsNameTextView.text = item.nameRu
                            actorsJobTextView.text = item.profession
                            Glide.with(staffImageView)
                                .load(item.posterUrl)
                                .placeholder(android.R.drawable.progress_indeterminate_horizontal)
                                .error(android.R.drawable.stat_notify_error)
                                .into(staffImageView)
                        }
                    }
                }
                launch {
                    viewModel.staffBestFilmFlow.collect {
                        topFilmsAdapter.setData(it)
                    }
                }
                launch {
                    viewModel.staffFilmographyFlow.collect {
                        binding.actorsFilmCountTextView.text =
                            getString(R.string.staff_detail_film_cout, it.toString())
                    }
                }
            }
        }
    }

    private fun showAllCollectionFilms() {
        val bundle: Bundle = Bundle().apply {
            putInt(StaffFilmographyFragment.Companion.STAFF_ID_ARG, staffId)
            putString(
                StaffFilmographyFragment.Companion.STAFF_NAME_ARG,
                binding.actorsNameTextView.text.toString()
            )
        }
        findNavController().navigate(
            R.id.action_staffDetailFragment_to_staffFilmographyFragment,
            bundle
        )
    }

    private fun showTopFilmCollection() {
        val bundle: Bundle = Bundle().apply {
            putParcelable(
                FILM_FETCH_TYPE_ARG,
                FilmDataToShare(
                    staffId,
                    FilmFetchType.STAFF_TOP_FILM,
                    null,
                    binding.actorsNameTextView.text.toString()
                )
            )
        }
        findNavController().navigate(R.id.action_staffDetailFragment_to_filmAllFragment, bundle)
    }

    private fun filmDetail(id: Int) {
        val bundle: Bundle = Bundle().apply {
            putInt(FILM_ID_ARG, id)
        }
        findNavController().navigate(R.id.action_staffDetailFragment_to_filmDetailsFragment, bundle)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: Int) =
            StaffDetailFragment().apply {
                arguments = Bundle().apply {
                    putInt(STAFF_ID_ARG, param1)
                }
            }

        const val STAFF_ID_ARG = "staff_id"
    }
}